package com.qys.training.biz.auth.service.impl;

import com.qys.training.biz.auth.entity.SysConfig;
import com.qys.training.biz.auth.mapper.SysConfigMapper;
import com.qys.training.biz.auth.service.HazelcastService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class HazelcastServiceImpl implements HazelcastService {
    @Resource
    private SysConfigMapper sysConfigMapper;

    @Override
    public int write(String key, String value) {
        List<SysConfig> select = sysConfigMapper.select(key);
        //如果key存在执行新增，不存在执行删除
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey(key);
        sysConfig.setConfigValue(value);
        if (select.size() > 0) {
            //update
            return sysConfigMapper.update(sysConfig);
        } else {
            //insert
            return sysConfigMapper.insert(sysConfig);
        }
    }

    @Override
    public String select(String key) {
        List<SysConfig> select = sysConfigMapper.select(key);
        if (select.size() > 0) {
            return select.get(0).getConfigValue();
        }
        return null;
    }

    @Override
    public int delete(String key) {
        return sysConfigMapper.delete(key);
    }
}
